package com.zzptc.easyshop.servlet;

import com.zzptc.easyshop.dao.GoodTypeDaoImp;
import com.zzptc.easyshop.dao.IDAO;
import com.zzptc.easyshop.dao.ProductDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "FirstPageServlet")
public class FirstPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter printWriter=response.getWriter();
        //printWriter.print("hello");
        //想：显示所有商品  dao获取所有商品   转到显示所有商品的页面  index.jsp
        IDAO dao=new ProductDao();
        IDAO daotype=new GoodTypeDaoImp();
        List listgood=null;
        List listgoodtype=null;
        //1、dao 获取所有商品
        try {
            listgood=dao.getAll();//dao 中有获取所有商品的方法
            listgoodtype=daotype.getAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //2、想： 在两个页面之间怎样传递数据 （所有商品  所有商品类型）  request
        request.setAttribute("allgood",listgood);
        request.setAttribute("allgoodtype",listgoodtype);

        //3、转到index.jsp
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
